package com.xmg.p2p.base.controller;

import com.xmg.p2p.base.domain.RealAuth;
import com.xmg.p2p.base.domain.Userinfo;
import com.xmg.p2p.base.service.IRealAuthService;
import com.xmg.p2p.base.service.IUserinfoService;
import com.xmg.p2p.base.util.FastDFSClient;
import com.xmg.p2p.base.util.JSONResult;
import com.xmg.p2p.base.util.RequireLogin;
import com.xmg.p2p.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by  zsl on  Date: 2017-11-08
 */
@Controller
public class RealAuthController {


    @Autowired
    private IUserinfoService userinfoService;
    @Autowired
    private IRealAuthService realAuthService;
     @Autowired
     private ServletContext servletContext;

    @RequireLogin
    @RequestMapping("realAuth")
    public String realAuth(Model model){
        Userinfo current=userinfoService.getCurrent();
        if(current.getIsRealAuth()){
            model.addAttribute("realAuth",realAuthService.get(current.getRealAuthId()));
            model.addAttribute("auditing",false);
            return "realAuth_result";
        }else {
            if(current.getRealAuthId()!=null){
                model.addAttribute("auditing",true);
                return "realAuth_result";
            }else {
                return "realAuth";
            }
        }
    }


    @RequestMapping("/realAuthUpload")
    @ResponseBody
public String realAuthUpload(MultipartFile file)
    {

        try {
            FastDFSClient fastDFSClient = new FastDFSClient("D:/idea/client.conf");
            String originalFilename = file.getOriginalFilename();
            String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            String url = fastDFSClient.uploadFile(file.getBytes(), extName);
            return "http://10.199.88.251/" + url;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    @RequestMapping("/realAuth_save")
    @ResponseBody
    public JSONResult realAuthSave(RealAuth realAuth){
realAuthService.apply(realAuth);
return new JSONResult();

    }

}
